package handlers

import (
	"eco-mall-mgr/models"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("title")
	author := c.PostForm("author")

	if title == "" || content == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}

	data := models.Commerce{
		Title:   title,
		Content: content,
		Author:  author,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {
	var commerces []models.Commerce
	models.DB.Debug().Find(&commerces)
	c.JSON(200, gin.H{
		"code": 200,
		"data": commerces,
		"msg":  "success",
	})
	return
}
